package com.jcwy.common.config;

import cn.hutool.core.collection.CollectionUtil;
import com.jcwy.common.IMysqlSyncPlugin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class MysqlSyncPluginConfig implements ApplicationContextAware {

    private ApplicationContext ctx = null;

    @Bean(name = "mysqlSyncPluginMap")
    public Map<String, IMysqlSyncPlugin> mysqlSyncPluginMap() {
        Map<String, IMysqlSyncPlugin> beans = ctx.getBeansOfType(IMysqlSyncPlugin.class);
        if (CollectionUtil.isEmpty(beans)) {
            return null;
        }

        Map<String, IMysqlSyncPlugin> pluginMap = new HashMap<>();
        for (Map.Entry<String, IMysqlSyncPlugin> item : beans.entrySet()) {
            pluginMap.put(item.getValue().tableName(), item.getValue());
            log.info("******** load mysql sync plugin success {} ********", item.getValue().tableName());
        }
        return pluginMap;

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx = applicationContext;
    }

}
